package com.yuan.plugin.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateHelper {

	/**
	 * 获取指定格式的今天的日期
	 * 
	 * @param format
	 * @return
	 */
	public static String getTodayDate(String format) {
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	/**
	 * 获取指定毫秒，指定格式的日期
	 * 
	 * @param sec
	 * @param format
	 * @return
	 */
	public static String getDateWithLong(long sec, String format) {
		Date date = new Date(sec);
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	/**
	 * 字符日期转日期
	 * 
	 * @return
	 */
	public static Date string2Date(String strDate, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		try {
			return formatter.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 日期转毫秒
	 * 
	 * @param str_date
	 * @return
	 */
	public static long date2Long(String str_date, String format) {
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(new SimpleDateFormat(format).parse(str_date));
			return c.getTimeInMillis();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	public static String TimeStamp2Date(String timestampString, String type) {
		Long timestamp = Long.parseLong(timestampString) * 1000;
		String date = new java.text.SimpleDateFormat(type)
				.format(new java.util.Date(timestamp));
		return date;
	}

	/**
	 * 日期大小比较
	 * 
	 * @param date1
	 *            20151212
	 * @param date2
	 *            20160102
	 * @return
	 */
	public static boolean dateCompareDate(String date1, String date2) {
		if (Long.parseLong(date1) >= Long.parseLong(date2))
			return true;
		else
			return false;
	}
}
